package assist.datastore.importer;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.logging.Logger;


import assist.datastore.JDODatastore;
import assist.datastore.parser.NHLEventsCSVParser;
import assist.datastore.parser.NHLGamesCSVParser;
import assist.datastore.parser.NHLPlayersCSVParser;
import assist.datastore.parser.NHLRostersCSVParser;
import assist.datastore.parser.NHLTeamCSVParser;
import assist.domain.model.Event;
import assist.domain.model.EventPlayerOnIce;
import assist.domain.model.Game;
import assist.domain.model.Player;
import assist.domain.model.RosterPlayer;
import assist.domain.model.Team;


/**
 * Temporary placeholder to demo old import code.
 * This used to be in the Servlet implementation, but took it out.
 * This is how I imported the data from CSV files to the SQL DB.
 */
public class Importer
{
    private static Logger logger = 
        Logger.getLogger(Importer.class.getName());    
    public String importTeams()
    {        
        NHLTeamCSVParser parser;
        try
        {
            parser = new NHLTeamCSVParser(
                    new File("data/Team.csv"));
            JDODatastore<Team> datastore = new JDODatastore<Team>();
            datastore.saveAll(parser);             
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
              
        return " Teams imported";       
    }    
    
    public Integer importPlayers(Integer startRecord)
    {        
        NHLPlayersCSVParser parser;
        try
        {                       
            parser = new NHLPlayersCSVParser(
                    new File("data/Player.csv"), startRecord);
            logger.info("Starting Import Players");    
            JDODatastore<Player> datastore = new JDODatastore<Player>();
            logger.info("Saving Players");    
            Collection<Player> saved = datastore.saveAll(parser);  
            logger.info("Done Saving Players"+ saved.size());   
            return parser.getNextRecordNum();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return -1;
          
    }    
    
        
    
    public Integer importGames(Integer startRecord)
    {        
        NHLGamesCSVParser parser;
        try
        {
            //System.err.println(new File(".").getAbsolutePath());
            
            parser = new NHLGamesCSVParser(
                    new File("data/Game.csv"), startRecord);
            JDODatastore<Game> datastore = new JDODatastore<Game>();
            Collection<Game> saved = datastore.saveAll(parser);  
            logger.info("Imported: " + saved.size());
            return parser.getNextRecordNum();  
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
              
        return -1;       
    }       
    
    public Integer importRosters(Integer startRecord)
    {        
        NHLRostersCSVParser parser = null;
        try
        {
            logger.info("Starting Import Rosters"); 
            parser = new NHLRostersCSVParser(
                    new File("data/GameRoster.csv"), startRecord);
            JDODatastore<RosterPlayer> datastore = new JDODatastore<RosterPlayer>();
            //datastore.saveAll(parser);   
            
            for (RosterPlayer player : parser)
            {
                try
                {
                    datastore.save(player);
                }
                catch(Exception e)
                {
                    logger.info(e.getMessage() + "," + player.getKey() + "," + player.getPlayer().getName());
                }
            }
            logger.info("Done Importing Rosters.");
            return parser.getNextRecordNum();    
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (javax.jdo.JDOUserException jdoException)
        {            
            //Throwable [] nested = jdoException.getNestedExceptions();
            jdoException.printStackTrace();
        }
              
        return -1;       
    }
    
    public Integer importEvents(Integer startRecord)
    {        
        NHLEventsCSVParser parser;
        try
        {
            logger.info("Starting Import Events");            
            parser = new NHLEventsCSVParser(
                    new File("data/GameEvent1000.csv"), startRecord);
            JDODatastore<Event> datastore = new JDODatastore<Event>();
            datastore.saveAll(parser);  
            JDODatastore<EventPlayerOnIce> datastore2 = new JDODatastore<EventPlayerOnIce>();
            datastore2.saveAll(parser.getEventPlayersOnIce());            
            logger.info("Done Importing Events.");
            return parser.getNextRecordNum();    
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
              
        return -1;       
    }      
}
